package com.badlogic.androidgames.mypirates.Sprite;

import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.Rect;

import com.badlogic.androidgames.framework.Game;
import com.badlogic.androidgames.framework.Graphics;

public class Player extends Ship {

	Bitmap whirlInTexture;
	Bitmap whirlOutTexture;
	
	Animation whirlInAnimation;
	Animation whirlOutAnimation;
	
	boolean isWhirling = false;
	
	public Cannon cannon;
	
	public Player(Game game) {
		super(game);
		// TODO Auto-generated constructor stub
	}
	
	public void Initialize() {
		super.Initialize("goodship.png", new Rect(0,0,50,50), new Point(1,8), new Point(5, 5));
		Graphics g = currentGame.getGraphics();
		whirlInTexture = g.newPixmap("goodshipwhirlin.png", null);
		whirlOutTexture = g.newPixmap("goodshipwhirlout.png", null);
	}
	
	@Override
	public void Update(float gameTime) {
		super.Update(gameTime);
		if(currentShipState == ShipState.Standby && movingAnimation.size() == 0 && isWhirling){
			currentShipState = ShipState.WhirlIn;
		}
		else if(currentShipState == ShipState.WhirlIn){
			if(whirlInAnimation.IsActive)
				whirlInAnimation.update();
			else{
				currentShipState = ShipState.WhirlOut;
			}
		}
		else if(currentShipState == ShipState.WhirlOut){
			if(whirlOutAnimation.IsActive)
				whirlOutAnimation.update();
			else{
				isWhirling = false;
				currentShipState = ShipState.Standby;
				IsActive = false;
				x = newX;
				y = newY;
			}
		}
		else if(currentShipState == ShipState.Attacking){
			cannon.update();
		}
	}
	
	@Override
	public void Draw(Graphics g) {
		if(currentShipState == ShipState.WhirlIn || currentShipState == ShipState.WhirlOut){
			if(whirlInAnimation.IsActive)
				whirlInAnimation.draw(g);
			else if(whirlOutAnimation.IsActive)
				whirlOutAnimation.draw(g);
		}
		else if(currentShipState == ShipState.Attacking){
			cannon.draw(g);
		}
		super.Draw(g);
	}
	
	public void MoveAndWhirl(Point d1, Point d2){
		super.Move(d1.x, d1.y);
		isWhirling = true;
		whirlInAnimation = new Animation(whirlInTexture, new Point(50, 50), new Point(32, 1), d1);
		whirlInAnimation.PlayFromFrameIndex(0);
		whirlOutAnimation = new Animation(whirlOutTexture, new Point(50,50), new Point(32,1), d2);
		whirlOutAnimation.PlayFromFrameIndex(0);
		this.newX = d2.x;
		this.newY = d2.y;
	}
	
	public void Fire(Point[] p, boolean[] isExploded){
		currentShipState = ShipState.Attacking;
		cannon = new Cannon(currentGame, this);
		cannon.Initialize();
		cannon.fire(p, isExploded);
	}
}
